<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Storage;

/**
 * 公共控制器
 */
class PublicController extends Controller{
	
	/**
	 * 用户登录
	 */
    public function Login(){
		
		//判断是否被安装
		if(!Storage::has('../Backup/install.lock')){
			 
			 $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/install.php';
			 
			 redirect($url,0,'还没有安装了YiPHP，请安装');
			 
			 die;
			 
        }
		
		//是否是POST提交
		if(IS_POST){
			
			$m_common_user = M('common_user');
			
			$map['username'] = I('param.username');
			
			//判断是否能注册
			$result = $m_common_user->where($map)->find();
			
				if(!$result){
					
					//没有注册去注册
					$this->error('用户名不存在,请注册后在登录。',U('Admin/Public/Register'));
				
				}else{        
				
					//查询用户是否存在
					$map['password'] = md5(I('param.password'));
					
					$result = $m_common_user->where($map)->find();

					if($result){
						
						
						//统一登录接口
						login($result['id']);
						
						//获得跳转着陆页
						$url = getLoginURL($result['id']);
						
						action_log('admin_public_login');//行为记录
						
						//跳转到着陆页
						$this->success('登录成功',U($url['url']));
			
					}else{
						
						//密码错误提示
						$this->error('密码错误,登录失败。');
					
					}
				}
				
		
		//显示视图
		}else{
			
			//页面配置
			$page = array(
				
				'title'=>'YiPHP 用户登录',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);

			$this->display();
			
		}
		
		
	}
	
	/**
	 * 用户注册
	 */
	public function Register(){
    
		if(IS_POST){
			
			$m_common_user = M('common_user');
			
			$map['username'] = I('param.username');
			
			//判断是否能注册
			$result = $m_common_user->where($map)->find();
			
			if($result){
				
				$this->success('用户名被占用请重新注册！');
			
			}else{      
			
				//判断密码是否一致
				$map['password'] = md5(I('param.password'));
				$confirm_password = md5(I('param.confirm_password'));
				
				if($map['password']!=$confirm_password){
					
					$this->error('两次密码不一致。');
					
				}else{
					
				
					$map['nickname'] = 'YiPHP用户';
					$map['tel'] = time();
					$map['group_id'] = 3; //默认归属用户组 将来用变量配置
					$map['email'] = time();
					$map['img'] = '/upload/m7q6o0o0o0o0o0of/2017/01/18/587ecd55dcea5.png';
					$map['create_time'] = time();
					$map['create_ip'] = get_client_ip();
					$map['status'] = 1;
					
					$result = $m_common_user->add($map);
					
					if($result){
						
						//统一登录接口
						login($result['id']);
						
						//获得跳转着陆页
						$url = getLoginURL($result['id']);
						
						action_log('admin_public_register');//行为记录
						
						//跳转到着陆页
						$this->success('登录成功',U($url['url']));
			
					}else{
						
						$this->error('注册失败,请联系客服处理。');
					
					}
				}
				
			}
			
		}else{
		
			//页面配置
			$page = array(
				
				'title'=>'YiPHP 用户注册',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
			
			$this->display();
			
		}
		
	}
	
	/**
	 * 手机登录
	 */
    public function TelLogin(){
		

		
		//是否是POST提交
		if(IS_POST){
			
			$m_common_user = M('common_user');
			$m_sms_code = M('sms_code');
			
			//判断验证码是否对
			$map['name'] = I('param.username');
			$map['code'] = I('param.code');
			
			$data['status'] = 1;
			//判断是否能注册
			$result = $m_sms_code->where($map)->save($data);
			
		
				if(!$result){
					
					//没有注册去注册
					$this->error('请输入正确的验证码！');
				
				}else{        
					
					$map_user['tel'] = I('param.username');
					$udata = $m_common_user->where($map_user)->find();
					//是用户直接登录
					if($udata){
						
						//统一登录接口
						login($udata['id']);
						
						//获得跳转着陆页
						$url = getLoginURL($udata['id']);
						
						action_log('admin_public_login');//行为记录
						
						//跳转到着陆页
						$this->success('登录成功',U($url['url']));
					
					//不是用户注册登录
					}else{
						
						
						$udatas['password'] = md5('123123q');
						$udatas['username'] = I('param.username');
						$udatas['nickname'] = 'YiPHP用户';
						$udatas['tel'] = I('param.username');
						$udatas['group_id'] = 3; //默认归属用户组 将来用变量配置
						$udatas['email'] = I('param.username');
						$udatas['img'] = '/upload/m7q6o0o0o0o0o0of/2017/01/18/587ecd55dcea5.png';
						$udatas['create_time'] = time();
						$udatas['create_ip'] = get_client_ip();
						$udatas['status'] = 1;
						
						$results = $m_common_user->add($udatas);
						
						if($results){
							
							//统一登录接口
							login($results);
							
							//获得跳转着陆页
							$url = getLoginURL($results);
							
							
							//跳转到着陆页
							$this->success('登录成功',U($url['url']));
				
						}else{
							
							$this->error('注册失败,请联系客服处理。');
						
						}
					
						
					}
					
				}
				
		
		//显示视图
		}else{
			
			//页面配置
			$page = array(
				
				'title'=>'YiPHP 用户登录',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);

			$this->display();
			
		}
		
		
	}
	
	/**
	 * 邮箱登录
	 */
    public function EmailLogin(){
		

		
		//是否是POST提交
		if(IS_POST){
			
			$m_common_user = M('common_user');
			$m_sms_code = M('sms_code');
			
			//判断验证码是否对
			$map['name'] = I('param.username');
			$map['code'] = I('param.code');
			
			$data['status'] = 1;
			//判断是否能注册
			$result = $m_sms_code->where($map)->save($data);
			
		
				if(!$result){
					
					//没有注册去注册
					$this->error('请输入正确的验证码！');
				
				}else{        
					
					$map_user['email'] = I('param.username');
					$udata = $m_common_user->where($map_user)->find();
					//是用户直接登录
					if($udata){
						
						//统一登录接口
						login($udata['id']);
						
						//获得跳转着陆页
						$url = getLoginURL($udata['id']);
						
						action_log('admin_public_login');//行为记录
						
						//跳转到着陆页
						$this->success('登录成功',U($url['url']));
					
					//不是用户注册登录
					}else{
						
						
						$udatas['username'] = I('param.username');
						$udatas['password'] = md5('123123q');
						$udatas['nickname'] = 'YiPHP用户';
						$udatas['tel'] = I('param.username');
						$udatas['group_id'] = 3; //默认归属用户组 将来用变量配置
						$udatas['email'] = I('param.username');
						$udatas['img'] = '/upload/m7q6o0o0o0o0o0of/2017/01/18/587ecd55dcea5.png';
						$udatas['create_time'] = time();
						$udatas['create_ip'] = get_client_ip();
						$udatas['status'] = 1;
						
						$results = $m_common_user->add($udatas);
						
						if($results){
							
							//统一登录接口
							login($results);
							
							//获得跳转着陆页
							$url = getLoginURL($results);
							
							
							//跳转到着陆页
							$this->success('登录成功',U($url['url']));
				
						}else{
							
							$this->error('注册失败,请联系客服处理。');
						
						}
					
						
					}
					
				}
				
		
		//显示视图
		}else{
			
			//页面配置
			$page = array(
				
				'title'=>'YiPHP 用户登录',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);

			$this->display();
			
		}
		
		
	}
	/**
	 * 用户退出
	 */
	public function Logout(){
		
		action_log('admin_public_logout');//行为记录
		
		cookie('uid',null);
		cookie('token',null);
		
		$this->success('退出成功',U('Admin/Public/Login'));
		
	}
	
}